package vote;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class VotePanel extends javax.swing.JPanel
{
    public VotePanel()
    {
        initComponents();
    }

    public void addRadioButtonsActionListener( ActionListener l )
    {
        Enumeration<AbstractButton> e = buttonGroup1.getElements();
        for( ; e.hasMoreElements(); )
        {
            e.nextElement().addActionListener( l );
        }
    }

    public String getSelectedButton()
    {
        Enumeration<AbstractButton> e = buttonGroup1.getElements();
        for( ; e.hasMoreElements(); )
        {
            JRadioButton b = (JRadioButton)e.nextElement();
            if( b.isSelected() )
            {
                return b.getText();
            }
        }
        return "";
    }

    public void updateRadioButtons( List<String> can )
    {
        this.candidates = can;
        updateComponents();
    }

    public void updateComponents()
    {
        buttonGroup1 = new ButtonGroup();
        jLabel1 = new JLabel();
        jLabel1.setText( "<html>WARNING! If you fail to vote now, the program will not let you vote again!<br>Choose the candidate:</html>" );
        GridLayout layout = new GridLayout( 0, 1 );
        this.add( jLabel1 );
        for( String name: candidates )
        {
            JRadioButton rb = new JRadioButton();
            rb.setText( name );
            this.add( rb );
            buttonGroup1.add( rb );
        }
        this.setLayout( layout );
    }

    public void initComponents()
    {
    }

    private List<String> candidates;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
}
